package com.neusoft.bizcore.web.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.web.config.BizcoreWebProperties.Swagger;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 配置swagger显示的接口
 *
 * @author naxiang
 */
@Configuration
@ConditionalOnProperty(name = "bizcore.web.swagger.enabled", havingValue = "true")
@EnableSwagger2
public class SwaggerConfiguration {
    @Autowired
    private BizcoreWebProperties properties;

    @Bean
    public Docket createRestApi() {
        final Swagger swagger = this.properties.getSwagger();
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(this.apiInfo(swagger)).select()
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)).paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo(final Swagger swagger) {
        return new ApiInfoBuilder().title(swagger.getTitle()).version(swagger.getVersion())
                .description(swagger.getDescription()).build();
    }
}
